package com.taotao.task;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.RedisUtils;
import com.taotao.service.ParamService;
import com.taotao.service.TaskService;

@Controller
public class SetGoodsTopTask {
	
	@Autowired
	TaskService taskService;
	@Autowired
	ParamService paramService;
	BaseResult baseResult = new BaseResult();
	
//    @Scheduled(cron = "0 */20 8-22 * * ? ") // 间隔10分钟执行
	@RequestMapping("/api/setGoodsTop")
	@ResponseBody
    public  Map<String, Object>  setGoodsTop() {
    	try {
    		boolean lock = RedisUtils.tryGetDistributedLock("lockKey_setGoodsTop", "setGoodsTop", 60000);
			if(lock){
				String param = paramService.getParam("job_SetGoodsTopTask");
	    		if("true".equals(param)){
	    			System.out.println("setGoodsTop执行开始...");
	        		taskService.setGoodsTop();
	        		System.out.println("setGoodsTop执行结束...");
	    		}	
				RedisUtils.releaseDistributedLock("lockKey_setGoodsTop", "setGoodsTop");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return  baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
    	return baseResult.getSuccMap();
    }
}